package com.catmiao.rdd.operate.transform;

import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.ArrayList;

/**
 * @author ChengMiao
 * @title: Transfer_01_Map
 * @projectName spark_study
 * @description: TODO
 * @date 2024/11/25 16:27
 */
public class Transfer_11_kv1 {

    public static void main(String[] args) throws InterruptedException {

        final SparkConf conf = new SparkConf();
        conf.setAppName("appName");
        conf.setMaster("local[*]");

        final JavaSparkContext jsc = new JavaSparkContext(conf);


        ArrayList<Integer> list = Lists.newArrayList(1, 2, 3, 4);

        JavaRDD<Integer> rdd = jsc.parallelize(list);


        // TODO 单值类型转 kv类型
        rdd.mapToPair(
                num -> new Tuple2<>(num,num*num)
        ).collect().forEach(System.out::println);


        jsc.close();


    }
}
